/**
 * Created by henian.xu on 2017/7/19.
 *
 */

class LocalStorage {
    constructor() {
        this.store = window.localStorage;
        this.prefix = 'hsl';
    }

    set(key, value, fn) {
        if (!key || typeof key === 'object') {
            throw new Error(
            'key为必填,且不能是object');
        }
        try {
            value = JSON.stringify(value);
        } catch (e) {}
        this.store.setItem(this.prefix + key, value);
        fn && fn();
    }

    get(key, fn) {
        if (!key || typeof key === 'object') {
            throw new Error(
                'key为必填,且不能是object');
        }
        let value = this.store.getItem(this.prefix + key);
        try {
            value = JSON.parse(value);
        } catch (e) {}
        return value;
    }

    remove(key) {
        if (!key || typeof key === 'object') {
            throw new Error(
                'key为必填,且不能是object');
        }
        this.store.removeItem(this.prefix + key);
    }
}

export default new LocalStorage();
